var scene = viewer.scene;

// var clipObjects = ['BIM', 'Point Cloud', 'Instanced', 'Model'];
// var viewModel = {
//     debugBoundingVolumesEnabled : false,
//     edgeStylingEnabled : true,
//     exampleTypes : clipObjects,
//     currentExampleType : clipObjects[0]
// };

var targetY = 0.0;
var planeEntities = [];
var selectedPlane;
var clippingPlanes;
console.log('------------')

//当鼠标按下时选择平面
var downHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
downHandler.setInputAction(function(movement) {
    var pickedObject = scene.pick(movement.position);
    if (Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        Cesium.defined(pickedObject.id.plane)) {
        selectedPlane = pickedObject.id.plane;
        selectedPlane.material = Cesium.Color.WHITE.withAlpha(0.05);
        selectedPlane.outlineColor = Cesium.Color.WHITE;
        scene.screenSpaceCameraController.enableInputs = false;
    }
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);

// Release plane on mouse up
//鼠标向上释放平面
var upHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
upHandler.setInputAction(function() {
    if (Cesium.defined(selectedPlane)) {
        selectedPlane.material = Cesium.Color.WHITE.withAlpha(0.1);
        selectedPlane.outlineColor = Cesium.Color.WHITE;
        selectedPlane = undefined;
    }

    scene.screenSpaceCameraController.enableInputs = true;
}, Cesium.ScreenSpaceEventType.LEFT_UP);

// Update plane on mouse move
//鼠标移动时更新平面
var moveHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
moveHandler.setInputAction(function(movement) {
    if (Cesium.defined(selectedPlane)) {
        var deltaY = movement.startPosition.y - movement.endPosition.y;
        targetY += deltaY;
    }
    console.log(targetY)
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

const createPlaneUpdateFunction = (plane) => {
    return function () {
        plane.distance = targetY;
        return plane;
    };
}


const loadTileset = () => {
    console.log('--')
    clippingPlanes = new Cesium.ClippingPlaneCollection({
        planes : [
            new Cesium.ClippingPlane(new Cesium.Cartesian3(0.0, 0.0, -1.0), 0.0)
        ],
        edgeColor: Cesium.Color.RED,
        edgeWidth: 1.0,
        unionClippingRegions: true, //true 才能多个切割
    });

    var tileset = window.titleset
    tileset.clippingPlanes = clippingPlanes;
    // tileset.debugShowBoundingVolume = viewModel.debugBoundingVolumesEnabled;
    return tileset.readyPromise.then(function() {
        var boundingSphere = tileset.boundingSphere;
        var radius = boundingSphere.radius;

        // viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.5, -0.2, radius * 4.0));

        for (var i = 0; i < clippingPlanes.length; ++i) {
            var plane = clippingPlanes.get(i);
            var planeEntity = viewer.entities.add({
                position : boundingSphere.center,
                plane : {
                    dimensions : new Cesium.Cartesian2(radius * 2.5, radius * 2.5),
                    material : Cesium.Color.WHITE.withAlpha(0.1),
                    plane : new Cesium.CallbackProperty(createPlaneUpdateFunction(plane), false),
                    outline : true,
                    outlineColor : Cesium.Color.WHITE
                }
            });
        }
        return tileset;
    }).catch(function(error) {
        console.log(error);
    });
}


/**
 * 名称：模型高亮
 * 作用：点击模型进行高亮
 * 状态：已完成
 * 参数：
 * @param
 *  负责人：杨世林
 */
const hightLight = () => {
    // 高亮元素
    const hightLighted = {
        feautre: undefined,
        originalColor: new Cesium.Color(),
    }

    viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(event) {
        // 清除之前的高亮元素
        if (Cesium.defined(hightLighted.feature)) {
            hightLighted.feature.color = hightLighted.originalColor;
            hightLighted.feature = undefined;
        }

        // 选择新要素
        const pickedFeature = viewer.scene.pick(event.position);
        console.log(pickedFeature)
        console.log(event.position)
        if (!Cesium.defined(pickedFeature)) {
            return;
        }

        // 存储选中要素的信息
        hightLighted.feature = pickedFeature;
        Cesium.Color.clone(
            pickedFeature.color,
            hightLighted.originalColor
        );
        // 高亮选中元素
        pickedFeature.color = Cesium.Color.RED.withAlpha(0.5);
        // pickedFeature.color = new Cesium.Color(0,0,0,0.5);
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN)
}




